﻿function TrialControllerInterface() {

    this.onActionDone = function (action) {
        return true;
    };

    this.processTutorial = function (time) {

    };
}

function TrialControllerImpl() {

    this.trial_status = 0;
    this.trial_meteorShut = 1;
    this.trial_meteorShut_start = 0;
    this.trial_meteorCollision = 2;
    this.trial_meteorCollision_start = 0;
    this.trial_fillFeul = 3;
    this.trial_fillFeul_done = 0;
    this.trial_fillFeulBySpaceShip = 4;
    this.trial_fillFeulBySpaceShip_done = 0;
    this.trial_maintenance = 5;
    this.trial_maintenance_done = 0;
    this.trial_repair = 6;
    this.trial_repair_done = 0;
    this.trial_success = 7;
    this.trial_success_done = 0;

    var currentTrialSession_endTime;

    this.onActionDone = function (action) {
        if (action == enumAction.REPAIR && this.trial_status == this.trial_repair) {
            this.trial_repair_done = 1;
            return true;
        }
        if (action == enumAction.MAINTENANCE && this.trial_status == this.trial_maintenance) {
            this.trial_maintenance_done = 1;
            return true;
        }
        if (action == enumAction.REFULING) {
            this.trial_fillFeul_done = 1;
            return true;
        }
        if (action == enumAction.REFULING_BY_FUEL_SHIP) {
            this.trial_fillFeulBySpaceShip_done = 1;
            return true;
        }

        return false;
    };
    

    this.processTutorial = function (time) {
        if (freezeShip) {
            return;
        }
        if (this.trial_status == undefined || this.trial_status == 0) {
            this.initTrial();
            this.trial_status = this.trial_meteorShut;
            return;
        }

        if (this.trial_status == this.trial_success) {
            return;
        }

        if (time >= 120000 || time >= getNewScenario().gameLength) { // go to real game after 120 seconds
            this.endTrial(time);
        }

        // trial_meteorShut
        if (this.trial_status == this.trial_meteorShut) {
            if (this.trial_meteorShut_start == undefined || this.trial_meteorShut_start == 0) {
                this.startMeteorsShut(time);
                this.trial_meteorShut_start = 1;
            } else {
                if (this.currentTrialSession_endTime <= time) {
                    this.trial_status = this.trial_meteorCollision;
                }
            }
            return;
        }

        // 1) trial_meteorCollision
        if (this.trial_status == this.trial_meteorCollision) {
            if (this.trial_meteorCollision_start == undefined || this.trial_meteorCollision_start == 0) {
                this.startMeteorCollision(time);
                this.trial_meteorCollision_start = 1;
            } else {
                if (this.currentTrialSession_endTime <= time) {
                    this.trial_status = this.trial_maintenance; // skipp on refuel 
                    this.startTrial_maintenance(time);
                }
            }
            return;
        }

        // 2) trial_fillFeul
        if (this.trial_status == this.trial_fillFeul) {
            if (this.trial_fillFeul_done == 1) {
                this.trial_status = this.trial_fillFeulBySpaceShip;
                this.startTrial_fillFeulBySpaceShip(time);
            }
            return;
        }

        // 3) trial_fillFeulBySpaceShip
        if (this.trial_status == this.trial_fillFeulBySpaceShip) {
            if (this.trial_fillFeulBySpaceShip_done == 1) {
                this.trial_status = this.trial_maintenance;
                this.startTrial_maintenance(time);
            }
            return;
        }

        // 4) trial_maintenance
        if (this.trial_status == this.trial_maintenance) {
            if (this.trial_maintenance_done == 1) {
                this.trial_status = this.trial_repair;
                this.startTrial_repair(time);
            }
            return;
        }
        if (this.trial_status == this.trial_repair) {
            if (this.trial_repair_done == 1) {
                this.endTrial(time);
            }
        }
    };

    this.initTrial = function() {
        this.trial_meteorShut = 1;
        this.trial_meteorCollision = 2;
        this.trial_fillFeul = 3;
        this.trial_fillFeulBySpaceShip = 4;
        this.trial_maintenance = 5;
        this.trial_repair = 6;
        this.trial_success = 7;

        getNewScenario().mechanicalEventSampler.setNextTime(999 * 1000);
        getNewScenario().fuelStationSampler.setNextTime(999 * 1000);
        this.writeTutorialMsg("Trial Game", "Press Ok to start the game");
    }

    this.startMeteorsShut = function(time) {
        this.writeTutorialMsg("Shoot down meteors", "Shoot down meteors by pressing the space-bar and control the ship with the arrow keys.");
        if (time == undefined) {
            time = 0;
        }
        this.currentTrialSession_endTime = time + (10 * 1000);
    }


    this.startMeteorCollision = function(time) {
        this.writeTutorialMsg("Collision with meteors", "Try to collide with a meteor in the following 10 seconds. See how you can lose points($) then.");
        this.currentTrialSession_endTime = time + (10 * 1000);
    }

    this.startTrial_fillFeul = function(time) {
        this.trial_fillFeul_done = 0;
        getNewScenario().fuelStationSampler.setNextTime(time + (5 * 1000));
        this.writeTutorialMsg("Fill the fuel tank", "Notice how the fuel percentage decreases in the fuel chart on the bottom-right side of the screen. You can fuel your spaceship by reaching a fuel station that will be shown in a few seconds.");
    }

    this.startTrial_fillFeulBySpaceShip = function(time) {
        this.trial_fillFeulBySpaceShip_done = 0;
        getNewScenario().fuelStationSampler.setNextTime(time + (999 * 1000));
        setFeulInventory(10000);
        this.writeTutorialMsg("Empty Fuel Tank", "When the fuel tank is empty (look at the fuel gauge on bottom-right of the screen), the spaceship will stop. The ship will be able to navigate once a fuel spaceship arrives at your ship and refills its tank. Make sure you get enough fuel in the fuel stations while flying.");
    }

    this.startTrial_maintenance = function(time) {
        this.trial_maintenance_done = 0;
        setFeulInventory(1000000);

        var costTable = getNewScenario().getCostTable();
        actionTime = costTable.getTime(enumAction.MAINTENANCE);
        actionCost = costTable.getCost(enumAction.MAINTENANCE);

        this.writeTutorialMsg("Maintenance Actions", "To reduce the probability of malfunctions, you should perform maintenance." +
                "<br><b>Maintenance duration: </b> " + (actionTime / 1000) + " seconds" +
                "<br><b>Cost: </b>" + actionCost + " points($)" +
        "<br>Press \"Enter\" or press the maintenance button on the top-right side of the screen to practice this action.");
    }

    this.startTrial_repair = function(time) {
        this.trial_repair_done = 0;
        getNewScenario().mechanicalEventSampler.setNextTime(time + (3 * 1000));
        var costTable = getNewScenario().getCostTable();
        var actionTime = costTable.getRepairTime(enumMC.MINOR);
        var actionCost = costTable.getRepairCost(enumMC.MINOR);
        this.writeTutorialMsg("Malfunction Occurrence", "Wait until a malfunction occurs. The malfunction will be repaired automatically." +
            "<br><b>Repair duration: </b> " + (actionTime / 1000) + " seconds" +
            "<br><b>Cost: </b>" + actionCost +" points($)");
    }

    this.endTrial = function(time) { // reset scenario parameters
        this.trial_status = this.trial_success;
        //currentCycle = [1000 + time, 10 + time, 15 + time, 20 + time];
        $("#dialog-message").html("The training game has been finished. The real 4 games will start now. It will take 2 minutes for the first game and 4 minutes for others.");
        $("#dialog-message").dialog({
            modal: false,
            title: "End training",
            width: 400,
            position: { my: "center", at: "center", of: $("#main") },
            buttons: [{
                text: "Ok",
                click: function () {
                    $(this).dialog("close");
                    window.location = window.url;
                }
            }]
        });
    }

    this.writeTutorialMsg = function(title, msg) {
        $("#dialog-message").html(msg);
        $("#dialog-message").dialog({
            modal: false,
            title: title,
            width: 400,
            position: { my: "center", at: "center", of: $("#main") },
            buttons: [{
                text: "Ok",
                closeOnEscape: false,
                click: function () {
                    $(this).dialog("close");
                    pauseGame = false;
                    timer.Timer.play();
                    animate();
                },
                open: function () {
                    $(".ui-dialog-titlebar-close").hide();
                    
                },
               

            }]
        });
        $(".ui-dialog :button").blur();
        pauseGame = true;
        timer.Timer.pause();
    }
}
TrialControllerImpl.prototype = new TrialControllerInterface();